package com.ruoyi.web.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.demo.domain.SysSchedulPaiRelate;
import com.ruoyi.web.controller.demo.domain.SysWzxUser;
import com.ruoyi.web.controller.mapper.SysSchedulPaiRelateMapper;
import com.ruoyi.web.controller.mapper.SysWzxUserMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.demo.domain.SysSchedulPai;
import com.ruoyi.web.controller.service.ISysSchedulPaiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * sys_schedul_paiController
 * 
 * @author ruoyi
 * @date 2022-12-27
 */
@Controller
@RequestMapping("/system/pai")
public class SysSchedulPaiController extends BaseController
{
    private String prefix = "system/pai";

    @Autowired
    private ISysSchedulPaiService sysSchedulPaiService;
    @Autowired
    private SysWzxUserMapper sysWzxUserMapper;
    @Autowired
    private SysSchedulPaiRelateMapper relate;
    @RequiresPermissions("system:pai:view")
    @GetMapping()
    public String pai()
    {
        return prefix + "/pai";
    }

    /**
     * 查询sys_schedul_pai列表
     */
    @RequiresPermissions("system:pai:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysSchedulPai sysSchedulPai)
    {
        startPage();
        List<SysSchedulPai> list = sysSchedulPaiService.selectSysSchedulPaiList(sysSchedulPai);
        return getDataTable(list);
    }

    /**
     * 导出sys_schedul_pai列表
     */
    @RequiresPermissions("system:pai:export")
    @Log(title = "sys_schedul_pai", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysSchedulPai sysSchedulPai)
    {
        List<SysSchedulPai> list = sysSchedulPaiService.selectSysSchedulPaiList(sysSchedulPai);
        ExcelUtil<SysSchedulPai> util = new ExcelUtil<SysSchedulPai>(SysSchedulPai.class);
        return util.exportExcel(list, "sys_schedul_pai数据");
    }

    /**
     * 新增sys_schedul_pai
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        SysUser sysUser = getSysUser();
        model.addAttribute("user",sysUser);
        List<SysWzxUser> sysWzxUsers = sysWzxUserMapper.selectSysWzxUserList(null);
        model.addAttribute("users",sysWzxUsers);
        return prefix + "/add";
    }

    /**
     * 新增保存sys_schedul_pai
     */
    @RequiresPermissions("system:pai:add")
    @Log(title = "sys_schedul_pai", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysSchedulPai sysSchedulPai) throws ParseException {
        sysSchedulPaiService.insertSysSchedulPai(sysSchedulPai);

        String[] beginTime = sysSchedulPai.getBeginTime();
        String[] endTime = sysSchedulPai.getEndTime();
        Long[] zhiId = sysSchedulPai.getZhiId();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");


        for (int i = 0; i < endTime.length; i++) {
            if(zhiId[i] != null){
                SysSchedulPaiRelate sysSchedulPaiRelate = new SysSchedulPaiRelate();
                sysSchedulPaiRelate.setPaiId(sysSchedulPai.getId());

                Date parse = simpleDateFormat.parse(beginTime[i]);
                Date parse1 = simpleDateFormat.parse(endTime[i]);

                sysSchedulPaiRelate.setBeginTime(parse);
                sysSchedulPaiRelate.setEndTime(parse1);
                sysSchedulPaiRelate.setZhiId(zhiId[i]);
                int i1 = relate.insertSysSchedulPaiRelate(sysSchedulPaiRelate);
                System.out.println("affecting"  + i1);
            }else{
                break;
            }
        }
        return toAjax(1);
    }

    /**
     * 修改sys_schedul_pai
     */
    @RequiresPermissions("system:pai:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysSchedulPai sysSchedulPai = sysSchedulPaiService.selectSysSchedulPaiById(id);
        mmap.put("sysSchedulPai", sysSchedulPai);
        SysUser sysUser = getSysUser();
        mmap.put("sysUser",sysUser);
        List<SysSchedulPaiRelate> relates = relate.selectSysSchedulPaiRelateById(id);
        mmap.put("relate",relates);
        List<SysWzxUser> sysWzxUsers = sysWzxUserMapper.selectSysWzxUserList(null);
        mmap.put("users",sysWzxUsers);
        return prefix + "/edit";
    }

    /**
     * 修改保存sys_schedul_pai
     */
    @RequiresPermissions("system:pai:edit")
    @Log(title = "sys_schedul_pai", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysSchedulPai sysSchedulPai)
    {
        return toAjax(sysSchedulPaiService.updateSysSchedulPai(sysSchedulPai));
    }

    /**
     * 删除sys_schedul_pai
     */
    @RequiresPermissions("system:pai:remove")
    @Log(title = "sys_schedul_pai", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysSchedulPaiService.deleteSysSchedulPaiByIds(ids));
    }
}
